//
//  EquipmentDetailViewController.m
//  LOL_PalmarHelper
//
//  Created by dllo on 15/6/19.
//  Copyright (c) 2015年 hantao. All rights reserved.
//

#import "EquipmentDetailViewController.h"

@interface EquipmentDetailViewController ()

@end

@implementation EquipmentDetailViewController


-(void)dealloc
{
    
    [self.rootScrollV release];
    [_needL release];
    [_needL1 release];
    [_firstImageV release];
    [_nameLabel release];
    [_priceLabel release];
    [_descriptionL release];
    [_descriptionL1 release];
    [_composeL release];
    [_composeV release];
    [super dealloc];



}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.rootScrollV = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.rootScrollV.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:self.rootScrollV];
    [_rootScrollV release];
    
    
    self.navigationItem.title = @"物品详情";
    self.model = [[EquipmentDetailModel alloc] init];
    [self requestDate];
    self.firstImageV = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x + 10, self.view.bounds.origin.y + 15, 85, 85)];
    self.firstImageV.backgroundColor = [UIColor whiteColor];
    [self.rootScrollV addSubview:self.firstImageV];
    [_firstImageV release];
    
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.firstImageV.frame.origin.x + self.firstImageV.frame.size.width + 15, self.firstImageV.frame.origin.y , self.view.frame.size.width - self.firstImageV.frame.origin.x - self.firstImageV.frame.size.width - 15, 40)];
    self.nameLabel.backgroundColor = [UIColor clearColor];
    self.nameLabel.font = [UIFont systemFontOfSize:20];
    [self.rootScrollV addSubview:self.nameLabel];
    [_nameLabel release];
    
    self.priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLabel.frame.origin.x, self.nameLabel.frame.origin.y + self.nameLabel.frame.size.height + 5, self.view.frame.size.width - self.firstImageV.frame.origin.x - self.firstImageV.frame.size.width - 20, 25)];
    self.priceLabel.backgroundColor = [UIColor clearColor];
    self.priceLabel.textColor = [UIColor lightGrayColor];
    self.priceLabel.font = [UIFont systemFontOfSize:15];
    [self.rootScrollV addSubview:self.priceLabel];
    [_priceLabel release];
    
    self.descriptionL = [[UILabel alloc] initWithFrame:CGRectMake(self.firstImageV.frame.origin.x, self.firstImageV.frame.origin.y + self.firstImageV.frame.size.height + 10, self.view.bounds.size.width - 20, 30)];
    self.descriptionL.backgroundColor = [UIColor clearColor];
    self.descriptionL.textColor = [UIColor lightGrayColor];
    self.descriptionL.text = @"物品属性";
    self.descriptionL.font = [UIFont systemFontOfSize:19];
    [self.rootScrollV addSubview:self.descriptionL];
    [_descriptionL release];
    
    self.descriptionL1 = [[UILabel alloc] initWithFrame:CGRectMake(self.descriptionL.frame.origin.x, self.descriptionL.frame.origin.y + self.descriptionL.frame.size.height + 5, self.descriptionL.frame.size.width, 50)];
    
    self.descriptionL1.backgroundColor = [UIColor whiteColor];
    self.descriptionL1.font = [UIFont systemFontOfSize:17];
    self.descriptionL1.layer.masksToBounds = YES;
    self.descriptionL1.layer.cornerRadius = 5;

    self.descriptionL1.numberOfLines = 0;
    [self.rootScrollV addSubview:self.descriptionL1];
    [_descriptionL1 release];
    
    self.needL = [[UILabel alloc] initWithFrame:CGRectMake(self.descriptionL1.frame.origin.x, self.descriptionL1.frame.origin.y + self.descriptionL1.frame.size.height + 10, self.descriptionL.frame.size.width, self.descriptionL.frame.size.height)];
    
    self.needL.backgroundColor = [UIColor clearColor];
       self.needL.textColor = [UIColor lightGrayColor];
    self.needL.text = @"需求";
    self.needL.font = [UIFont systemFontOfSize:19];
    [self.rootScrollV addSubview:self.needL];
    [_needL release];
    
    
    self.needL1 = [[UIScrollView alloc] initWithFrame:CGRectMake(self.needL.frame.origin.x, self.needL.frame.origin.y + self.needL.frame.size.height + 5, self.needL.frame.size.width, 50)];
    
    self.needL1.backgroundColor = [UIColor whiteColor];
    self.needL1.bounces = NO;
    self.needL1.showsHorizontalScrollIndicator = NO;
    self.needL1.layer.masksToBounds = YES;

    self.needL1.layer.cornerRadius = 5;
    [self.rootScrollV addSubview:self.needL1];
    [_needL1 release];
    
    
    self.composeL = [[UILabel alloc] initWithFrame:CGRectMake(self.needL1.frame.origin.x, self.needL1.frame.origin.y + self.needL1.frame.size.height + 10, self.needL.frame.size.width, self.needL.frame.size.height)];
    self.composeL.backgroundColor = [UIColor clearColor];
    self.composeL.textColor = [UIColor lightGrayColor];
    self.composeL.text = @"可合成";
    self.composeL.font = [UIFont systemFontOfSize:19];
    [self.rootScrollV addSubview:self.composeL];
    [_composeL release];

    self.composeV = [[UIScrollView alloc] initWithFrame:CGRectMake(self.composeL.frame.origin.x, self.composeL.frame.origin.y + self.composeL.frame.size.height + 5, self.needL1.frame.size.width, 50)];
    self.composeV.bounces = NO;
    self.composeV.showsHorizontalScrollIndicator = NO;
    self.composeV.backgroundColor = [UIColor whiteColor];
    self.composeV.layer.masksToBounds = YES;
    self.composeV.layer.cornerRadius = 5;
    [self.rootScrollV addSubview:self.composeV];
    [_composeV release];
    
    
}



-(CGFloat)heightOfMe:(NSString *)text
{
    
    CGSize size = CGSizeMake(self.descriptionL.frame.size.width, 10000);
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17] forKey:NSFontAttributeName];
    CGRect frame = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];;
    
    return frame.size.height;
    
    
}



-(void)requestDate
{

    
    NSString *url = [NSString stringWithFormat:@"http://lolbox.duowan.com/phone/apiItemDetail.php?id=%@",self.equipmentID];
    
[AFN AFNGetData:url andBlock:^(id respondes) {
    
    if (![respondes isKindOfClass:[@"error" class]]) {
        
   
        
        NSMutableDictionary *dic = respondes;
        
        [self.model setValuesForKeysWithDictionary:dic];
        self.model.ID = [dic objectForKey:@"id"];
        self.model.desc = [dic objectForKey:@"description"];

        [self.firstImageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/zb/%@_64x64.png",self.equipmentID]] placeholderImage:[UIImage imageNamed:@"picholder@2x.png"]];
        
        
        self.nameLabel.text = self.model.name;
        NSString *str = [NSString stringWithFormat:@"价格:%@ 总价:%@ 售价:%@",self.model.price,self.model.allPrice,self.model.sellPrice];
        self.priceLabel.text = str;
        self.descriptionL1.text = self.model.desc;
        
        CGRect frame = self.descriptionL1.frame;
        CGFloat hei = [self heightOfMe:self.descriptionL1.text];
        frame.size.height = hei + 10;
        self.descriptionL1.frame = frame;;
        
        self.needL.frame = CGRectMake(self.descriptionL1.frame.origin.x, self.descriptionL1.frame.origin.y + self.descriptionL1.frame.size.height + 10, self.descriptionL.frame.size.width, self.descriptionL.frame.size.height);
        
        self.needL1.frame = CGRectMake(self.needL.frame.origin.x, self.needL.frame.origin.y + self.needL.frame.size.height + 5, self.needL.frame.size.width, 60);
        
        NSString * need = [dic objectForKey:@"need"];
        self.array = [need componentsSeparatedByString:@","];
        
        for (int i = 0; i < self.array.count; i++) {
            
            UIButton *image = [UIButton buttonWithType:UIButtonTypeCustom];
            image.frame =CGRectMake(5 * (i + 1) + (i * 50), 5, 50, 50);
            image.tag = 1000 + i;
            NSString *url = [self.array objectAtIndex:i];
            NSString *url1 = [NSString stringWithFormat:@"http://img.lolbox.duowan.com/zb/%@_64x64.png",url];
            image.backgroundColor = [UIColor clearColor];
            
            [DownLoanImage downLoadImageUrl:url1 block:^(NSData *data) {
                
                [image setImage:[UIImage imageWithData:data] forState:UIControlStateNormal];
                
            }];
            
            [image addTarget:self action:@selector(buttonACtionNeed:) forControlEvents:UIControlEventTouchUpInside];
            
            [self.needL1 addSubview:image];
            
            
            
        }
        
        self.needL1.contentSize = CGSizeMake( self.array1.count * 50 + self.array1.count * 5,5);
        
        self.composeL.frame = CGRectMake(self.needL1.frame.origin.x, self.needL1.frame.origin.y + self.needL1.frame.size.height + 10, self.needL.frame.size.width, self.needL.frame.size.height);
        
        self.composeV.frame = CGRectMake(self.composeL.frame.origin.x, self.composeL.frame.origin.y + self.composeL.frame.size.height + 5, self.needL1.frame.size.width, self.needL1.frame.size.height);
        
        NSString * compose = [dic objectForKey:@"compose"];
        self.array1 = [compose componentsSeparatedByString:@","];
        for (int i = 0; i < self.array1.count; i++) {
            
            UIButton *image = [UIButton buttonWithType:UIButtonTypeCustom];
            image.frame =CGRectMake(5 * (i + 1) + (i * 50), 5, 50, 50);

            image.tag = 2000 + i;
            NSString *url = [self.array1 objectAtIndex:i];
            NSString *url1 = [NSString stringWithFormat:@"http://img.lolbox.duowan.com/zb/%@_64x64.png",url];
            image.backgroundColor = [UIColor clearColor];
            
            [DownLoanImage downLoadImageUrl:url1 block:^(NSData *data) {
               
                [image setImage:[UIImage imageWithData:data] forState:UIControlStateNormal];
                
            }];
            
            [image addTarget:self action:@selector(buttonACtionCompose:) forControlEvents:UIControlEventTouchUpInside];
            
            [self.composeV addSubview:image];
            
            
            
        }

        self.composeV.contentSize = CGSizeMake( self.array1.count * 50 + self.array1.count * 5,5);

        self.rootScrollV.contentSize = CGSizeMake(0, self.composeV.frame.origin.y + self.composeV.frame.size.height + 64 + 49 + 30);
        
        
    }else if ([respondes isEqualToString:@"error"]) {
    
        UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [self.view addSubview:al];
        [al show];
        [al release];

    
    }
    
    
    
    
    
    
    
    
    
}];

}





-(void)buttonACtionNeed:(UIButton *)button
{

   NSString *s = [self.array objectAtIndex: button.tag - 1000];
    
    EquipmentDetailViewController *eq = [[EquipmentDetailViewController alloc] init];
    
    eq.equipmentID = s;
    
    [self.navigationController pushViewController:eq animated:YES];

}

-(void)buttonACtionCompose:(UIButton *)button
{

    NSString *s = [self.array1 objectAtIndex: button.tag - 2000];
    
    EquipmentDetailViewController *eq = [[EquipmentDetailViewController alloc] init];
    
    eq.equipmentID = s;
    
    [self.navigationController pushViewController:eq animated:YES];

    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
